package com.wmc.exception;

import com.wmc.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlovalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.error("服务器异常");
    }
    @ExceptionHandler
    public Result handleDuplicatekeyException(DuplicateKeyException e) {
        log.error("服务器异常",e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String msg = message.substring(i);
        String[] s = msg.split(" ");

        return Result.error(s[2]+"已存在");
    }
    @ExceptionHandler(RuntimeException.class)
    public Result handleBusinessException(RuntimeException e) {
        return Result.error(e.getMessage());
    }

}
